package www.ym.com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.R;
import www.ym.com.entity.Dish;
import www.ym.com.entity.Setmeal;
import www.ym.com.entity.ShoppingCart;
import www.ym.com.service.DishService;
import www.ym.com.service.SetmealService;
import www.ym.com.service.ShoppingCartService;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 添加至购物车
     *
     * @return
     */
    @PostMapping("/add")
    public R<String> addShopCart(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        if (shoppingCart.getSetmealId() == null) {
            //增加套餐到购物车

            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId()).ne(ShoppingCart::getUserId, shoppingCart.getUserId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if (one == null) {
                //该用户还未添加套餐至购物车中
                shoppingCartService.save(shoppingCart);
            } else {
                one.setCopies(one.getCopies() + 1);
                one.setMoney(one.getMoney() + shoppingCart.getMoney());
                shoppingCartService.updateById(one);
            }
        } else {
            //增加菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId()).eq(ShoppingCart::getUserId, shoppingCart.getUserId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if (one == null) {
                //该用户还未添加菜品至购物车中
                shoppingCartService.save(shoppingCart);
            } else {
                //判断口味是否一致
                if (shoppingCart.getDishFlavor() == one.getDishFlavor()) {
                    one.setCopies(one.getCopies() + 1);
                    one.setMoney(one.getMoney() + shoppingCart.getMoney());
                    shoppingCartService.updateById(one);
                }
                //口味不一致
                else {
                    shoppingCartService.save(shoppingCart);
                }
            }
        }
        return R.success("", "添加购物车成功");
    }

    /**
     * 增加或者减少购物车数量
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/changeCopies")
    public R<String> changeCopies(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart byId = shoppingCartService.getById(shoppingCart.getId());
        if (byId.getDishId() == null) {
            //添加套餐
            Setmeal byId1 = setmealService.getById(byId.getSetmealId());
            shoppingCart.setMoney(byId1.getPrice() * shoppingCart.getCopies());
        } else {
            //添加菜品
            Dish byId1 = dishService.getById(byId.getDishId());
            shoppingCart.setMoney(byId1.getPrice() * shoppingCart.getCopies());

        }
        if (shoppingCart.getCopies() == 0) {
            shoppingCartService.removeById(shoppingCart.getId());
        } else {
            //增加套餐或者菜品的数量
            shoppingCartService.updateById(shoppingCart);
        }
        return R.success("", "处理成功");
    }

    /**
     * 删除购物车的东西
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R<String> del(Long id) {
        shoppingCartService.removeById(id);
        return R.success("", "删除成功");
    }

    /**
     * 购物车列表
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(Long id) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, id);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list, "处理成功");
    }

    //修改购物车
//    @PutMapping("/update")
//    public R<String> update(@RequestBody ShoppingCart shoppingCart) {
//        ShoppingCart byId = shoppingCartService.getById(shoppingCart.getId());
//
//        //判断用户是否删除菜品或套餐
//        if (shoppingCart.getCopies() != 0) {
//            byId.setCopies(shoppingCart.getCopies());
//            byId.setMoney(shoppingCart.getMoney());
//            shoppingCartService.updateById(byId);
//        } else {
//            shoppingCartService.removeById(shoppingCart.getId());
//        }
//        return R.success("", "处理成功");
//    }


//    @Autowired
//    private ShoppingCartService shoppingCartService;
//
//    /**
//     * 新增菜品和套餐至購物車中
//     *
//     * @param shoppingCart
//     */
//    @PostMapping("/add")
//    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
//        //設置用戶id
//        Long currentId = BaseContext.getCurrentId();
//        shoppingCart.setUserId(currentId);
//
//        //查詢當前菜品和套餐是否存在，已經存在則數量加1
//        //查詢當前菜品是否在購物車(數據庫)中
//        Long dishId = shoppingCart.getDishId();
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId, currentId);
//
//        //添加的菜品信息
//        if (dishId != null) {
//            queryWrapper.eq(ShoppingCart::getDishId, dishId);
//        }
//        //添加的套餐信息
//        else {
//            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
//        }
//        //查詢當前套餐和菜品是否在購物車中
//        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
//
//        //存在則數量+1
//        if (cart != null) {
//            cart.setNumber(cart.getNumber() + 1);
//            shoppingCartService.updateById(cart);
//        }
//        //不存在，添加默認數量為1
//        else {
//            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
//            shoppingCartService.save(shoppingCart);
//            cart = shoppingCart;
//        }
//
//        return R.success(cart);
//    }
//
//    /**
//     * 减少购物车中菜品或者套餐的数量
//     * @param shoppingCart
//     * @return
//     */
//    @PostMapping("/sub")
//    public R<ShoppingCart> reduce(@RequestBody ShoppingCart shoppingCart){
//        Long currentId = BaseContext.getCurrentId();
//        shoppingCart.setUserId(currentId);
//
//        //查詢當前菜品和套餐是否存在，已經存在則數量加1
//        //查詢當前菜品是否在購物車(數據庫)中
//        Long dishId = shoppingCart.getDishId();
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId, currentId);
//
//        //添加的菜品信息
//        if (dishId != null) {
//            queryWrapper.eq(ShoppingCart::getDishId, dishId);
//        }
//        //添加的套餐信息
//        else {
//            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
//        }
//        //查詢當前套餐和菜品是否在購物車中
//        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
//
//        //存在則數量+1
//        if (cart != null) {
//            cart.setNumber(cart.getNumber() -1);
//            if (cart.getNumber()==0){
//                shoppingCartService.removeById(cart);
//            }
//            shoppingCartService.updateById(cart);
//        }
//        return R.success(cart);
//    }
//
//    /**
//     * 购物车列表
//     *
//     * @return
//     */
//    @GetMapping("/list")
//    public R<List<ShoppingCart>> list() {
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
//        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
//        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
//        return R.success(list);
//    }
//
//
//    /**
//     * 删除购物车
//     * @return
//     */
//    @DeleteMapping("/clean")
//    public R<String> clean(){
//        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
//        shoppingCartService.remove(queryWrapper);
//        return R.success("清空购物车成功");
//    }


}
